<template>
  <div>
    <dynamic-crud title="故障退款" :data="list" :option="option"
      :tableLoading="tableLoading" :disabled="isView" :rowAdd="rowadd"></dynamic-crud>
    <div class="bottom-menu" v-if="!isView">
      <el-button type="primary" size="small" :disabled="btn" @click="handelSubmit" :loading="isSubmit">保 存</el-button>
      <el-button size="small" @click="handleCancle" :loading="isSubmit">取 消</el-button>
    </div>
  </div>
</template>

<script>
  import {fetchList, addObj, putObj} from '@/api/subwaystm/swstinputpassengerwork/refund-work';
  import { dateFormat } from '@/util/date';
  import {validateDeviceOperateNo} from "@/util/validate";
  export default {
    name: 'refund-work',
    inject: ['passengerWork'],
    data() {
      return {
        list: [],
        isSubmit: false,
        tableLoading: false,
        relationId: null,
        btn: false,
        option: {
          column: [{
            label: '事件详情',
            prop: 'failureOperator',
            type: 'select',
            dicUrl: '/admin/dict/type/stm_failure_operator',
            cell: true,
	    filterable: true
          }, {
            label: '故障次数',
            prop: 'failureTimes',
            type: 'number',
            cell: true
          }, {
            label: '退款金额',
            prop: 'amountRefunded',
            type: 'number',
            precision: 2,
            cell: true,
            step:0
          }, {
            label: '上传截图',
            prop: 'filePath',
            type: 'upload',
            component: 'zd-upload',
            cell: true,
            hide: true
          }, {
            label: '事务发生时间',
            prop: 'workTime',
            type: 'datetime',
            format: 'yyyy-MM-dd HH:mm:ss',
            valueFormat: 'yyyy-MM-dd HH:mm:ss',
            cell: true,
            width: 210
          }]
        }
      }
    },
    computed: {
      isView(){
        return this.boxType == 'view'
      }
    },
    props: {
      workInfo: Object,
      boxType: String
    },
    methods: {
      rowadd: function(callback){
        let now = new Date()
        if(now.getHours() < 6){
          now.setDate(now.getDate() - 1)
        }
        callback({
          "workTime":dateFormat(now)
        })
      },
      getList(){
        if(this.relationId != this.workInfo.id){
          this.list = []
          this.tableLoading = true
          fetchList(this.workInfo.id).then(response => {
            this.relationId = this.workInfo.id
            this.list = response.data.data
            this.list.forEach(l => l.$cellEdit = true)
            this.tableLoading = false
          })
        }
      },
      clear(){
        this.list = []
      },
      handelSubmit(){
        var params = {
          ...this.workInfo,
          workList: this.list
        }
        if(this.boxType == 'add'){
          this.passengerWork.validateForm((valid)=>{
            if(valid){
              if (!validateDeviceOperateNo(this.workInfo.deviceOperateNum)) {
                this.$message.error("设备操作号必须为09开头的6位纯数字！");
                return;
              }
              if(this.list.length == 0){
                this.$message.info('请添加故障退款')
                return
              }else{
                for(const item of this.list){
                  if(item.failureOperator == null || item.failureTimes ==null || item.amountRefunded ==null || item.workTime == null){
                    this.$message.info("事件详情、故障次数、退款金额、事务发生时间为必填项，不能为空！")
                    return
                  }
                  if (item.amountRefunded > 0) {
                    this.$message.info("请检查当前事件的金额应该为负数！");
                    return;
                  }
                }
              }
              this.isSubmit = true
              addObj(params).then(response => {
                this.$message.success('添加成功')
                this.btn = true
                this.isSubmit = false
		this.passengerWork.getList(this.passengerWork.page)//20240827 测试  点击保存成功后刷新主页面
              })
            }
          })
        }else{
          this.passengerWork.validateForm((valid)=>{
            if(valid){
              if(this.list.length > 0){
                for(const item of this.list){
                  if(item.failureOperator == null || item.failureTimes ==null || item.amountRefunded ==null || item.workTime == null){
                    this.$message.info("事件详情、故障次数、退款金额、事务发生时间为必填项，不能为空！")
                    return
                  }
                  if (item.amountRefunded > 0) {
                    this.$message.info("请检查当前事件的金额应该为负数！");
                    return;
                  }
                }
              }
              this.isSubmit = true
              putObj(params).then(response => {
                this.$message.success('修改成功')
                this.btn = true
                this.isSubmit = false
              })
            }
          })


        }
      },
      handleCancle(){
        if(this.boxType == 'mergeEdit'){
          this.passengerWork.closeDialogMerge()
        }else{
          this.passengerWork.closeDialog()
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
  .bottom-menu{
    margin-top: 30px;
    display: flex;
    justify-content: center
  }
</style>
